package histogram;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import global.GlobalConsts;

public class MyTripleKey implements WritableComparable<MyTripleKey> {

	private IntWritable datasetId;
	private Text joinAttr;
	private IntWritable binId;
	
	public MyTripleKey() {
		datasetId = new IntWritable(0);
		joinAttr = new Text();
		binId = new IntWritable(0);
	}
	
	public MyTripleKey(int datasetId, String joinAttr, int binId) {
		this.datasetId = new IntWritable(datasetId);
		this.joinAttr = new Text(joinAttr);
		this.binId = new IntWritable(binId);
	}
	
	public int getDatasetId() {
		return datasetId.get();
	}
	
	public String getJoinAttr() {
		return joinAttr.toString();
	}
	
	public int getBinId() {
		return binId.get();
	}
	
	public void setDatasetId(int datasetId) {
		this.datasetId.set(datasetId);
	}
	
	public void setJoinAttr(String joinAttr) {
		this.joinAttr.set(joinAttr);
	}
	
	public void setBinId(int binId) {
		this.binId.set(binId);
	}
	
	@Override
	public void write(DataOutput out) throws IOException {
		datasetId.write(out);
		joinAttr.write(out);
		binId.write(out);
	}
	
	@Override
	public void readFields(DataInput in) throws IOException {
		datasetId.readFields(in);
		joinAttr.readFields(in);
		binId.readFields(in);
	}
	
	@Override
	public String toString() {
		return getDatasetId() + GlobalConsts.fieldSeparator + getBinId()
				+ GlobalConsts.fieldSeparator + getJoinAttr();
	}
	
	@Override
	public int hashCode() {
		return binId.hashCode();
	}
	
	@Override
	public int compareTo(MyTripleKey o) {
		int res = this.datasetId.compareTo(o.datasetId);
		if (res == 0) {
			res = this.binId.compareTo(o.binId);
			if (res == 0) {
				res = this.getJoinAttr().compareToIgnoreCase(o.getJoinAttr());
			}
		}
		return res;
	}

}
